package com.tomatozq163.sharding.config;

import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.driver.api.yaml.YamlShardingSphereDataSourceFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import javax.sql.DataSource;
import java.io.File;

@Slf4j
@Configuration
public class DataSourceConfig {
    @Bean
    public DataSource createDataSource(){
        System.out.println("createDataSource");
        
        DataSource dataSource = null;

        try {
            Resource resource = new ClassPathResource("ShardingSphere.yml");
            File yamlFile = resource.getFile();

            dataSource = YamlShardingSphereDataSourceFactory.createDataSource(yamlFile);
        }
        catch (Exception ex){
            log.error(ex.getMessage(),ex);
        }

        return dataSource;
    }
}
